import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_module/plugin/func_interface.dart';
import 'package:flutter_module/plugin/globalParam.dart';

class PluginUtil extends FunctionInterface implements GlobalParam {
  PluginUtil._();

  static PluginUtil _instance;

  static PluginUtil getInstance() {
    if (_instance == null) {
      _instance = PluginUtil._();
    }
    return _instance;
  }

  static const basicPlatform = const BasicMessageChannel(GlobalParam.Basic_Plugin, StandardMessageCodec());
  static const methodPlatform = const MethodChannel(GlobalParam.Method_Plugin);
  static const eventPlatform = const EventChannel(GlobalParam.Event_Plugin);

  /// event 发送
  Future<String> sendMessage(dynamic msg) async {
    String reply = await basicPlatform.send(msg);
    print(reply);
    return reply;
  }

  /// event 接收
  void receiveMessage() {
    basicPlatform.setMessageHandler((message) async {
      print(message);
      return "is ok";
    });
  }

  void startActivity(String activityName) async {
    Map<String, String> map = {"data": activityName};
    String result =
        await methodPlatform.invokeMethod(GlobalParam.Start_Activity, map);
    print(result);
  }

  void showToast(String msg) async {
    Map<String, String> map = {"data": "Flutter Hello !"};
    String result =
        await methodPlatform.invokeMethod(GlobalParam.Show_Toast, map);
    print(result);
  }

  void showLog(String msg) async {
    Map<String, String> map = {"data": "Flutter Hello !"};
    String result =
        await methodPlatform.invokeMethod(GlobalParam.Show_Log, map);
    print(result);
  }

  void startFlutter(BuildContext context, Widget widget) {
    Navigator.of(context).push(MaterialPageRoute(builder: (builderContext) {
      return widget;
    }));
  }
}
